package org.androidpn.activity;

import org.androidpn.R;
import org.androidpn.client.ServiceManager;
import org.androidpn.http.HttpApi;
import org.androidpn.model.CUser;
import org.androidpn.model.CUserInfo;
import org.androidpn.model.NetModel;
import org.androidpn.utils.ConstanceUtil;
import org.androidpn.utils.JsonUtil;
import org.androidpn.utils.ToastUtil;

import com.android.volley.VolleyError;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.TextView;

public class LoginActivity extends BaseActivity implements View.OnClickListener {
	// 消息提示
	private static final int MSG_LOGIN_SHOW_LOADING = 0x01;
	private static final int MSG_LOGIN_HIDE_LOADING = 0x02;

	private TextView fastRigiste;
	private EditText etPhone;
	private EditText etPwd;
	private TextView tvLogin;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_login);

		fastRigiste = (TextView) findViewById(R.id.fast_registe);
		etPhone = (EditText) findViewById(R.id.et_phone);
		etPwd = (EditText) findViewById(R.id.et_pwd);
		tvLogin = (TextView) findViewById(R.id.tv_login);
		fastRigiste.setOnClickListener(this);
		tvLogin.setOnClickListener(this);

		// start the service
//		ServiceManager serviceManager = new ServiceManager(this);
//		serviceManager.setNotificationIcon(R.drawable.notification);
//		serviceManager.startService();
		// 测试数据
		etPhone.setText("15700000050");
		etPwd.setText("123456");
	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.fast_registe:
			Intent intent = new Intent(this, RegisterActivity.class);
			startActivity(intent);
			break;
		case R.id.tv_login:
			String phone = etPhone.getText().toString();
			String password = etPwd.getText().toString();
			CUser cUser = new CUser();
			cUser.setTelephone(phone);
			cUser.setPassword(password);
			if (TextUtils.isEmpty(phone) || TextUtils.isEmpty(password)) {
				ToastUtil.showShortToast(LoginActivity.this, "电话和密码不能为空");
				return;
			}

			sendEmptydMessage(MSG_LOGIN_SHOW_LOADING);
			MyResponseListener myResponseListener = new MyResponseListener();
			HttpApi.login(cUser, myResponseListener, myResponseListener);
			break;
		}
	}

	class MyResponseListener
			implements com.android.volley.Response.Listener<String>, com.android.volley.Response.ErrorListener {

		@Override
		public void onResponse(String response) {
			// 隐藏提示框
			sendEmptydMessage(MSG_LOGIN_HIDE_LOADING);
			NetModel netModel = JsonUtil.parseObject(response, NetModel.class);
			if (netModel.getCode() == ConstanceUtil.REQUEST_SUCCESS) {

				CUserInfo cUserInfo = JsonUtil.parseObject(netModel.getContent(), CUserInfo.class);

				Intent intentMain = new Intent(LoginActivity.this, MainActivity.class);
				intentMain.putExtra("cuser", cUserInfo);
				startActivity(intentMain);
				LoginActivity.this.finish();
			} else if (netModel.getCode() == ConstanceUtil.REQUEST_EMPTY) {
				CUser cUser = JsonUtil.parseObject(netModel.getContent(), CUser.class);
				Intent intent = new Intent(LoginActivity.this, CompleteUserinfoActivity.class);
				intent.putExtra("cuser", cUser);
				startActivity(intent);
			} else {
				ToastUtil.showShortToast(LoginActivity.this, netModel.getContent());
			}
		}

		@Override
		public void onErrorResponse(VolleyError error) {
			// 显示提示框
			sendEmptydMessage(MSG_LOGIN_HIDE_LOADING);
			ToastUtil.showShortToast(LoginActivity.this, "网络异常");
		}
	}

	@Override
	protected void onHandleMessage(Message msg) {
		switch (msg.what) {
		case MSG_LOGIN_SHOW_LOADING: {
			showProgressDialog(getString(R.string.login_loading));
			break;
		}
		case MSG_LOGIN_HIDE_LOADING: {
			hideProgressDialog();
			break;
		}
		default:
			break;
		}
	}

}
